package com.infoplatform.daoimpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session; 
import org.hibernate.SessionFactory;

import com.infoplatform.bean.SecondaryCommodity;
import com.infoplatform.bean.SecondarycommodityComment;
import com.infoplatform.bean.Student;
import com.infoplatform.dao.SecondaryDao;

public class SecondaryDaoImpl implements SecondaryDao {

	private SessionFactory sessionFactory = null;
	
	@SuppressWarnings("unchecked")
	public List<SecondaryCommodity> SearchCommodity(String secondaryTag) {
		
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		String queryStr = "from SecondaryCommodity ";
		if(!secondaryTag.equals("all"))
		{
			queryStr += "where tag='"+secondaryTag + "'";;
		}
		Query query = session.createQuery(queryStr);
		List<SecondaryCommodity> secondaryList = (List<SecondaryCommodity>)query.list();
		session.getTransaction().commit();
		session.close();
		return secondaryList;
	}
	@SuppressWarnings("unchecked")
	public List<SecondaryCommodity> SearchCommodityById(Integer id) {
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		String queryStr = "from SecondaryCommodity where commodityId="+id;
		Query query = session.createQuery(queryStr);
		List<SecondaryCommodity> secondaryList = (List<SecondaryCommodity>)query.list();
		session.getTransaction().commit();
		session.close();
		return secondaryList;
	}
	public void AddSecondary(SecondaryCommodity second) {
		
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.save(second);
		session.getTransaction().commit();
		session.close();
	}
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	@SuppressWarnings("unchecked")
	public List<SecondaryCommodity> getTop8() {
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		String queryStr = "from SecondaryCommodity ";
		Query resultSet = session.createQuery(queryStr);
		resultSet.setFirstResult(0);
		resultSet.setMaxResults(4);
		List<SecondaryCommodity> top8 = resultSet.list();
		session.getTransaction().commit();
		session.close();
		return top8;
	}
	@SuppressWarnings("unchecked")
	public List<Student> QueryCustomer(Integer customerId) {
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		String queryStr = "from Student ";
		Query resultSet = session.createQuery(queryStr);
		List<Student> showCustomer = (List<Student>)resultSet.list();
		session.getTransaction().commit();
		session.close();
		return showCustomer;
	}
	
	@SuppressWarnings("unchecked")
	public List<Student> DeleteCustomer(Integer customerId) {
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		Student student = new Student();
		student = (Student) session.get(Student.class, (int)customerId);
		session.delete(student);
		String queryStr = "from Student ";
		Query resultSet = session.createQuery(queryStr);
		List<Student> showCustomer = (List<Student>)resultSet.list();
		session.getTransaction().commit();
		session.close();
		return showCustomer;
	}
	@SuppressWarnings("unchecked")
	public List<SecondaryCommodity> DeleteCommodityById(Integer id) {
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		SecondaryCommodity second = new SecondaryCommodity();
		second = (SecondaryCommodity) session.get(SecondaryCommodity.class, (int)id);
		session.delete(second);
		String queryStr = "from SecondaryCommodity ";
		Query resultSet = session.createQuery(queryStr);
		List<SecondaryCommodity> showSecondaryCommodity = (List<SecondaryCommodity>)resultSet.list();
		session.getTransaction().commit();
		session.close();
		return showSecondaryCommodity;
	}
	@SuppressWarnings("unchecked")
	public List<SecondaryCommodity> QueryCommodity(String secondaryTag) {
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		String queryStr = "from SecondaryCommodity where tag='"+secondaryTag + "'";
		Query query = session.createQuery(queryStr);
		List<SecondaryCommodity> secondaryList = (List<SecondaryCommodity>)query.list();
		session.getTransaction().commit();
		session.close();
		return secondaryList;
	}
	@SuppressWarnings("unchecked")
	public Map<String, SecondaryCommodity> SearchMap(String secondaryTag) {
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		String sql = "from Student b,SecondaryCommodity a where a.studentId=b.studentId and a.tag='" + secondaryTag + "'";
		ArrayList list = (ArrayList)session.createQuery(sql).list();
		Iterator iter = list.iterator();
		Map<String,SecondaryCommodity> searchMap = new HashMap<String,SecondaryCommodity>();
		while(iter.hasNext())
		{
			Object o[] = (Object[]) iter.next();
			Student stu = (Student)o[0];
			SecondaryCommodity second = (SecondaryCommodity)o[1];
			searchMap.put(stu.getName() + "(" + stu.getStudentId() + ")", second);
		}
		return searchMap;
	}
	@SuppressWarnings("unchecked")
	public List<SecondaryCommodity> SearchCommodity_New(String secondaryTag,
			Integer personalId) {
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		String queryStr = "from SecondaryCommodity ";
		if(!secondaryTag.equals("all"))
		{
			queryStr += "where tag='"+secondaryTag + "'and studentId =" + personalId;
		}
		Query query = session.createQuery(queryStr);
		List<SecondaryCommodity> secondaryList = (List<SecondaryCommodity>)query.list();
		session.getTransaction().commit();
		session.close();
		return secondaryList;
	}
	@SuppressWarnings("unchecked")
	public Map<String, SecondarycommodityComment> SearchComment(
			Integer commodityId) {
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		String sql = "from Student b,SecondarycommodityComment a where a.studentId=b.studentId and a.commodityId=" + commodityId ;
		ArrayList list = (ArrayList)session.createQuery(sql).list();
		Iterator iter = list.iterator();
		Map<String,SecondarycommodityComment> searchMap = new HashMap<String,SecondarycommodityComment>();
		while(iter.hasNext())
		{
			Object o[] = (Object[]) iter.next();
			Student stu = (Student)o[0];
			SecondarycommodityComment second = (SecondarycommodityComment)o[1];
			searchMap.put(stu.getName() + "(" + stu.getStudentId() + ")", second);
		}
		return searchMap;
	}
	
	@SuppressWarnings("unchecked")
	public Map<String, SecondaryCommodity> EverythingSearch(
			String searchCondition,String sortType) {
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		String sql = "from Student b,SecondaryCommodity a where a.studentId=b.studentId and a.name like '%" + searchCondition + "%'";
		
		if(sortType != null && sortType.equals("guanzhu"))
		{
			sql += "order by a.attentionCount desc";
		}
		else if(sortType != null && sortType.equals("jiage"))
		{
			sql += "order by a.price desc";
		}
		System.out.println(sql);
		ArrayList list = (ArrayList)session.createQuery(sql).list();
		Iterator iter = list.iterator();
		Map<String,SecondaryCommodity> searchMap = new HashMap<String,SecondaryCommodity>();
		while(iter.hasNext())
		{
			Object o[] = (Object[]) iter.next();
			Student stu = (Student)o[0];
			SecondaryCommodity second = (SecondaryCommodity)o[1];
			searchMap.put(stu.getName() + "(" + stu.getStudentId() + ")", second);
			System.out.println(second.getName());
		}
		return searchMap;
	}
}
